/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.utils;

import com.funcom.peeler.UpdateCont;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class WorkBenchToolBar extends JPanel{
    private JPanel mainPanel;
    private JCheckBox snapToGrid;
    private JSlider gridSize;
    private JCheckBox gridEnable;
    private JTextField gridSizeNumber;
    private int grid_step = 32;

    /**
     * Toolbar for Grid settings
     */
    public WorkBenchToolBar(){
        add(mainPanel);
        initListeners();

        gridSizeNumber.setText(String.valueOf(grid_step));
    }

    private void initListeners(){
        gridEnable.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                setGridEnabled(gridEnable.isSelected());
           }
       });

        gridSize.addChangeListener( new ChangeListener() {
            public void stateChanged( ChangeEvent e ) {
                gridSize.setValue( 2 * (gridSize.getValue() / 2 ));
                setGridStep( gridSize.getValue() );
                gridSizeNumber.setText( String.valueOf( gridSize.getValue() ) );
            }
        });

        gridSizeNumber.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                gridSize.setValue(Integer.parseInt(gridSizeNumber.getText()));
                gridSizeNumber.setText(String.valueOf(gridSizeNumber.getText()));
            }
        });

        snapToGrid.addFocusListener(new FocusListener(){
            @Override
            public void focusGained(FocusEvent e) {
                setSnapEnabled( snapToGrid.isSelected() );
                UpdateCont.get().getFocusedWorkBench().getBui().getCanvas().requestFocus();
            }
            @Override
            public void focusLost(FocusEvent e) {

            }
        });
    }

    /**
     *
     * @return int Grid size
     */
    public int getGridStep() {
        return grid_step;
    }

    /**
     * Sets new grid size
     * @param new_step Size to set
     */
    public void setGridStep( int new_step ) {
        grid_step = Math.max( 1, new_step );
    }

    /**
     *
     * @return boolean
     */
    public boolean snapEnabled(){
        return snapToGrid.isSelected();
    }

    /**
     *
     * @return boolean
     */
    public boolean gridEnabled() {
        return gridEnable.isSelected();
    }

    /**
     * Toggle snap to grid
     * @param enabled
     */
    public void setSnapEnabled( boolean enabled ){
        if(enabled){
            snapToGrid.setSelected(true);
        }else snapToGrid.setSelected(false);
    }

    /**
     * Toggle grid visible
     * @param enabled
     */
    public void setGridEnabled( boolean enabled ) {

        if ( enabled ) {
            gridEnable.setSelected( true );
            gridSize.setEnabled( true );
        }
        else {
            gridEnable.setSelected( false );
            gridSize.setEnabled( false );
        }
    }
}
